﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.DataViz.NetworkDiagram.QueryControls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class NumberFieldFilter : UserControl, IFieldFilter
    {
        /// <summary>
        /// 
        /// </summary>
        public NumberFieldFilter()
        {
            InitializeComponent();
            this.Load += new EventHandler(NumberFieldFilter_Load);
        }

        void NumberFieldFilter_Load(object sender, EventArgs e)
        {
            this.lbl_FieldName.Text = this.Stat.FieldName;
            if(this.Stat.DataType==SimpleDataType.IntNumberType)
            {
                this.num_From.DecimalPlaces = 0;
                this.num_To.DecimalPlaces = 0;
                this.num_From.Increment = 1;
                this.num_To.Increment = 1;
            }
            this.num_From.Minimum = decimal.Parse(this.Stat.MinValue.ToString());
            this.num_From.Maximum = decimal.Parse(this.Stat.MaxValue.ToString());
            this.num_To.Minimum = decimal.Parse(this.Stat.MinValue.ToString());
            this.num_To.Maximum = decimal.Parse(this.Stat.MaxValue.ToString());
            this.num_From.Value = decimal.Parse(this.Stat.MinValue.ToString());
            this.num_To.Value = decimal.Parse(this.Stat.MaxValue.ToString());
        }

        #region Implementation of IFieldFilter
        private FieldStat _Stat;
        /// <summary>
        /// 
        /// </summary>
        public FieldStat Stat
        {
            get
            {
                return _Stat;
            }
            set
            {
                _Stat = value;
            }
        }

        private string _NetworkName;
        /// <summary>
        /// 
        /// </summary>
        public string NetworkName
        {
            get
            {
                return _NetworkName;
            }
            set
            {
                _NetworkName = value;
            }
        }

        public event OnFieldFilterUpdated FieldFilterUpdated;

        #endregion

        private void num_From_ValueChanged(object sender, EventArgs e)
        {
            decimal max = (decimal) this.num_To.Value;
            decimal min = (decimal) this.num_From.Value;
            if (this.FieldFilterUpdated != null)
                this.FieldFilterUpdated(this.NetworkName, this.Stat.FieldName, min, max, null);
        }

        private void num_To_ValueChanged(object sender, EventArgs e)
        {
            decimal max = (decimal)this.num_To.Value;
            decimal min = (decimal)this.num_From.Value;
            if (this.FieldFilterUpdated != null)
                this.FieldFilterUpdated(this.NetworkName, this.Stat.FieldName, min, max, null);
        }
    }
}
